<?php

class Ordinary_maintenance extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Ordinary_maintenance':
	 * @var integer $id
	 * @var integer $fk_maintain_truck_id
	 * @var integer $fk_truck_id
	 * @var integer $fk_driver_id
	 * @var double $odometer_count
	 * @var integer $max_item
	 * @var integer $mean_item
	 * @var integer $smallest_item
	 * @var double $maintain_cost
	 * @var string $maintain_co
	 * @var string $maintain_date
	 * @var string $tyre_name
	 * @var integer $tyre_position
	 * @var integer $tyre_type
	 * @var integer $tyre_sum
	 * @var double $tyre_unit_price
	 * @var string $tyre_no
	 * @var integer $state
	 * @var string $create_time
	 */
	
	public $plate_no;
	public $maintain_plate_no;
	public $driver_name;
	public $maintain_reg_no;
	public $reg_no;
	public $_item=array('0'=>array('name'=>'更换轮胎'),
					   '1'=>array('name'=>'发动机','info'=>array('0'=>'高压油泵','1'=>'大修','2'=>'发电机','3'=>'启动机','4'=>'气泵',
					   		'5'=>'水箱','6'=>'水泵','7'=>'增压机','8'=>'方向机')),
					   '2'=>array('name'=>'底盘','info'=>array('9'=>'离合器','10'=>'后桥','11'=>'刹车系统','12'=>'齿轮油')),
				       '3'=>array('name'=>'拖车','info'=>array('13'=>'支架','14'=>'刹车','15'=>'轮骨轴头')),	
					   '4'=>array('name'=>'更换机油'),
					   '5'=>array('name'=>'更换齿轮油'),
					   '6'=>array('name'=>'更换蓄电池'),
					   '7'=>array('name'=>'其他'),
	);
	private $_kind=array('0'=>'车辆','1'=>'尾板');
	private $_tyre_position=array('0'=>'主机1-2','1'=>'主机1-6','2'=>'主机3-6','3'=>'尾板1-4','4'=>'尾板1-8','5'=>'尾板1-12','6'=>'尾板5-8',
	'7'=>'尾板9-12');
	private $_tyre_type=array('0'=>'900-20','1'=>'1000-20','2'=>'1100-20');
	private $_max_item=array('0'=>'维修','1'=>'保养','2'=>'更新');
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ordinary_maintenance';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('maintain_co','length','max'=>100),
			array('remark','length','max'=>500),
			array('max_item, mean_item, smallest_item', 'numerical', 'integerOnly'=>true),
			array('odometer_count, maintain_cost', 'numerical'),
			array('driver_name,maintain_reg_no,reg_no,kind,max_item,mean_item,maintain_cost,
			      maintain_co,maintain_date,plate_no', 'required'),
			array('maintain_cost', 'match','pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('odometer_count', 'match','pattern' => '/^\d{1,8}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多8位，小数位2位'),
			array('maintain_date', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
			
			array('tyre_name,tyre_position,tyre_type,tyre_sum,tyre_unit_price,tyre_no', 'required','on'=>'tyre'),
			array('tyre_sum,tyre_type,tyre_position', 'numerical', 'integerOnly'=>true,'on'=>'tyre'),
			array('tyre_unit_price', 'match','pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位','on'=>'tyre'),
			array('tyre_name','length','max'=>30,'on'=>'tyre'),
			array('tyre_no','length','max'=>500,'on'=>'tyre'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		if($this->mean_item!=0){ //没有更换轮胎
			$this->tyre_name='';
			$this->tyre_sum=0;
			$this->tyre_unit_price=0;
			$this->tyre_position=0;
			$this->tyre_type=0;
			$this->tyre_no='';
		}	
		return true;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'fk_maintain_truck_id' => '维修车辆/尾板id',
			'kind' => '车辆/尾板',
			'fk_truck_id' => '费用所属车辆id',
			'fk_driver_id' => '司机id',
			'odometer_count' => '里程表读数',
			'max_item' => '维修方式',//1:保养  0:维修
			'mean_item' => '维修项目',//0:更换轮胎  1:发动机  2:底盘  3:拖车  4:更换机油  5:更换齿轮油  6:更换蓄电池  7:其他
			'smallest_item' => '',//维修小项  '0'=>'高压油泵','1'=>'大修','2'=>'发电机','3'=>'启动机','4'=>'气泵',
								  //'5'=>'水箱','6'=>'水泵','7'=>'增压机','8'=>'方向机','9'=>'离合器','10'=>'后桥',
								  //'11'=>'刹车系统','12'=>'齿轮油','13'=>'支架','14'=>'刹车','15'=>'轮骨轴头'	
			'maintain_cost' => '维修金额(元)',
			'maintain_co' => '维修单位',
			'maintain_date' => '维修日期',
			'tyre_name' => '轮胎名称',
			'tyre_position' => '轮胎安装位置',//0-11: 主机1-12
			'tyre_type' => '轮胎规格',//0:900-20 1:1000-20 2:1100-20
			'tyre_sum' => '轮胎数量(个)',
			'tyre_unit_price' => '轮胎单价(元)',
			'tyre_no' => '轮胎号',
			'state' => '状态',
			'create_time' => '创建时间',
			'maintain_plate_no' =>'车辆/尾板牌号',
			'plate_no' =>'费用所属车牌号',
			'driver_name' =>'司机姓名',
			'reg_no' =>'费用所属车编号',
			'maintain_reg_no' =>'登记编号',
			'remark' => '备注'
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'maintain_reg_no'=>array('B',20,'format'=>'0'),
			'maintain_plate_no'=>array('C',20),
			'kind'=>array('D',15,'getKindName'),
			'odometer_count'=>array('E',15),
			'max_item'=>array('F',15,'getMaxItemName'),
			'mean_item'=>array('G',15,'getMeanItemName'),
			'driver_name'=>array('H',15),
			'plate_no'=>array('I',23),
			'reg_no'=>array('J',20,'format'=>'0'),
			'maintain_cost'=>array('K',15),
			'maintain_date'=>array('L',10),
		);
	}
	
	public function getTyrePositionNames(){
		return $this->_tyre_position;
	}
	
	public function getTyrePositionName($n=null){
		$name='';
		if($n!=null)
			$name=$this->_tyre_position[$n];
		else	
			if($this->mean_item==0)
				$name=$this->_tyre_position[$this->tyre_position];  
		return $name;
	}
	
	public function getTyreTypeNames(){
		return $this->_tyre_type;
	}
	
	public function getTyreTypeName($n=null){
		$name='';
		if($n!=null)
			$name=$this->_tyre_type[$n];
		else	
			if($this->mean_item==0)
				$name=$this->_tyre_type[$this->tyre_type];  
		return $name;
	}
	
	public function getKindNames(){
		return $this->_kind;
	}
	
	public function getKindName($n=null){
		return $this->_kind[$n==null?$this->kind:$n];
	}
	
	public function getMeanItemNames(){
		$names=array();
		foreach($this->_item as $n=>$val){
			$names[$n]=$val['name'];
		}
		return $names;
	}
	
	public function getMeanItemName($n=null){
		return $this->_item[$n==null?$this->mean_item:$n]['name'];
	}
	
	public function getSmallestItemNames(){
		$names=array();
		foreach($this->_item as $n=>$val){
			if(!isset($val['info'])) continue;
			foreach($val['info'] as $i=>$va){
				$names[$i]=$va;
			}
		}
		return $names;
	}
	
	public function getSmallestItemName($n=null){
		$name='';
		if($n!=null)
			foreach($this->_item as $val){
				if(!isset($val['info'])) continue;
				foreach($val['info'] as $i=>$va){
					if($i==$n) {$name=$va;break;}
				}
			}
		else
			if(isset($this->_item[$this->mean_item]['info'][$this->smallest_item]))
				$name=$this->_item[$this->mean_item]['info'][$this->smallest_item];	
		return $name;
	}
	
	public function getMeanItemHasNum(){
		$has=array();
		$script="var has=[];";
		foreach($this->_item as $n=>$val){
			if(!isset($val['info'])) continue;
			$script.="has[$n]=[];";
			foreach($val['info'] as $i=>$va){
				$script.="has[$n].push($i);";
			}
		}
		$script.="var sel2=[];";
		foreach($this->getSmallestItemNames() as $val){
			$script.="sel2.push('$val');";
		}
		return $script;
	}
	
	public function getMaxItemNames(){
		return $this->_max_item;
	}
	
	public function getMaxItemName($n=null){
		return $this->_max_item[$n==null?$this->max_item:$n];
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='maintain_plate_no';
		$params[]='plate_no';
		$params[]='driver_name';
		$params[]='reg_no';
		$params[]='maintain_reg_no';
		return $params;
	}
}